<template>
  <div class="login">
    <div>
      <el-input placeholder="请输入用户名" v-model="name" clearable class="input_style"></el-input>
      <span v-if="error.name" class="err-msg">{{error.name}}</span>
    </div>
    <div>
      <el-input placeholder="请输入密码" v-model="pwd" show-password class="input_style"></el-input>
      <span v-if="error.pwd" class="err-msg">{{error.pwd}}</span>
    </div>
    <div>
      <el-button type="primary" @click="login" class="login_style">登录</el-button>
    </div>

    <div>
      <el-button type="secondary" @click="getUserList" class="login_style">GetUserList</el-button>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",
  data(){
    return {
      name: 'alex',
      pwd : '123456',
      error : {
        name: '',
        pwd : ''
      }
    }
  },
  methods:{
    login(){
      let that = this

      const { name, pwd, $router} = this
      // this.$router.push({
      //   name: "Main",
      //   params: {
      //     username: this.name
      //   }
      // });

      const url = "http://localhost:8090/sys/user/login1"
      axios.post(url, {
        username: this.name,
        password: this.pwd
      }).then(res => {
        console.log(res.data)
        console.log(res.data.data)
        console.log(res.data.data.token)
        const token = res.data.data.token
        if (token != null || token != "") {
          console.log("token: ", token)
          this.$cookie.set("token", token)
          let tokenString = this.$jwt.decode(token)
          console.log("tokenString: ", tokenString)

          that.$store.commit("set_token", token)
          that.$store.commit("setUserInfo", "test");

          this.$notify({
            title: "登录成功",
            type: "success",
            showClose: false,
            duration: 1000
          });
        }
        this.$cookie.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")
      })
    },

    getUserList() {
      const url = "http://localhost:8090/sys/user/list"
      axios.get(url, {
        headers: {
          'content-type': 'application/json',
          "token":'14a1347f412b39f'  //token换成从缓存获取
        }
      }).then(res => {
        console.log(res.data)
      })
    }
  }
}
</script>

<style scoped>
.login{
  margin-top: 200px;
}
.input_style{
  width: 200px;
  margin-bottom: 10px;
}
.login_style{
  width: 200px;
}
</style>